package com.goodwill.androidtest.thermometer;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.goodwill.androidtest.R;
import com.olaser.manager.DKUsbManager;
import com.olaser.usb_connect.HidProto;
import com.olaser.utils.DKSoundPlayer;

public class ThermometerActivity extends AppCompatActivity {

    private int selfTestState;
    private TextView tipLabel;
    private TextView tempText;
    private Switch modeSw;
    private int mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thermometer);
//        degreeLabel = findViewById(R.id.degree_label);
//        tempText = findViewById(R.id.tempText);
//        tipLabel = findViewById(R.id.tip_label);
//        modeSw = findViewById(R.id.modeSw);
        initView();
        DKUsbManager.shareInstance().initCommunication(this);

        initCallBack();
    }

    private void initView() {
        tipLabel = findViewById(R.id.tipLabel);
        tempText = findViewById(R.id.tempText);
        Button startBtn = findViewById(R.id.button);
        modeSw = findViewById(R.id.modeSw);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startGetTemperature();
            }
        });
        modeSw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mode = 1;

                } else {
                    mode = 0;
                }
                if (DKUsbManager.shareInstance().canRecord()) {

                    setWorkMode();
                }

            }



        });
    }

    private void setWorkMode() {

        DKUsbManager.shareInstance().controlSetProto(new HidProto.SetWorkMode(mode));

    }

    private void initCallBack() {


        DKUsbManager.shareInstance().setCb(new DKUsbManager.DKUsbManagerCallBack() {
            @Override
            public void receivedTemperature(final float temperature, float ambient, float board) {
                runOnUiThread(new Runnable() {
                    @SuppressLint("DefaultLocale")
                    @Override
                    public void run() {
                        tempText.setText(String.format("%.1f°C", temperature));
                        tipLabel.setText("Done");
                    }
                });
            }

            @Override
            public void receivedTemperatureDataError(int code, String msg) {


            }

            @Override
            public void onAttachSuccess(final String msg) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tipLabel.setText(msg);

                    }
                });
            }

            @Override
            public void receivedTemperatureTimeout(int state) {

            }

            @Override
            public void onAttachFailed(int code, final String msg) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tipLabel.setText(msg);

                    }
                });
            }

            @Override
            public void onDetachSuccess(final String msg) {
                runOnUiThread(new Runnable() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void run() {
                        tipLabel.setText("Device Disconnected");

                    }
                });
            }


            @Override
            public void receivedWorkMode(final int state) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        mode = state;
                        if(mode == 1)
                            modeSw.setChecked(true);
                        else
                            modeSw.setChecked(false);
                    }
                });

            }


            @Override
            public void onSendSuccess(byte[] bytes, String msg) {


            }

            @Override
            public void onSendFailed(byte[] bytes, String msg) {


            }


            @Override
            public void receivedSelfTestStatus(int state) {
                if (state == 0) {
                    runOnUiThread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            tipLabel.setText("Ready");
                        }
                    });
                } else if (state == -1) {
                    runOnUiThread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            tipLabel.setText("TEMP SENSOR FAULT");
                        }
                    });
                } else if (state == -2) {
                    runOnUiThread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            tipLabel.setText("PROXIMITY FAULT");
                        }
                    });
                } else if (state == -3) {
                    runOnUiThread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            tipLabel.setText("UNKNOWN FAULT");
                        }
                    });

                }
            }
        });


    }


    void startGetTemperature() {
        if (!DKUsbManager.shareInstance().canRecord() || DKUsbManager.shareInstance().is_in_recording()) {
            return;
        }
        int mode = 1; // for body mode measure
        // int mode = 0; // for surface mode temperature

        DKUsbManager.shareInstance().startMeasure();

        if (DKUsbManager.shareInstance().canRecord()) {

            runOnUiThread(new Runnable() {
                @SuppressLint("SetTextI18n")
                @Override
                public void run() {
                    tipLabel.setText("Measuring");
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @SuppressLint("SetTextI18n")
                @Override
                public void run() {
                    tipLabel.setText("not Ready");
                }
            });

        }

    }
}
